博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JSP丶新闻发布会系统
阅读量:6821 次
发布时间:2019-06-26

本文共 8054 字,大约阅读时间需要 26 分钟。

新闻发布会

项目所需要的一些实现类 servlet 工具类

 

1.实现登录功能

 

前端界面的代码

 

 

 

1 
2
3
4
5
6
7
8
9

登录实现类代码

1 public boolean loginGetBool(Admin admin) { 2       rs=  executeSelect("select *from admin where name=? and \"pwd\"=?",admin.getAname(),admin.getApwd()); 3       try { 4         if(rs.next()){ 5               return true; 6           } 7       } catch (SQLException e) { 8         // TODO Auto-generated catch block 9         e.printStackTrace();10       }11       return false;12     }

登录servlet

public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        //接收请求时的编码utf-8          request.setCharacterEncoding("utf-8");          response.setContentType("text/html;charset=utf-8");          String  name=request.getParameter("uname");          String  pwd=request.getParameter("upwd");                    Admin admin=new Admin(name,pwd);                System.out.println(admin.getAname());                              AdminDaoImpl adi=new AdminDaoImpl();                String dbn=adi.login(admin);                             if(dbn!=null){                                         Cookie cookie=new Cookie("unameCookie",name);                    cookie.setMaxAge(60*60*24);                                        response.addCookie(cookie);                                    System.out.println("登陆成功!");                    HttpSession session= request.getSession();                    session.setAttribute("uname", name);                    session.setMaxInactiveInterval(60*10);                                        response.sendRedirect(request.getContextPath()+"/newspages/admin.jsp");                }else{                    response.sendRedirect(request.getContextPath()+"/index.jsp");                }    }

2.实现新增新闻

 

新增实现类方法

1 public boolean addNews(News news) { 2         Date date=new Date(); 3         DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 4         Date time = null; 5         try { 6             time = format.parse(format.format(date)); 7         } catch (ParseException e) { 8             // TODO Auto-generated catch block 9             e.printStackTrace();10         }11         12             Object[] obj={news.getNauthor(),news.getNcontent(),time,null,news.getNtitle(),news.getNtypeid()};13     14         15       16       return  executeUpdate("INSERT INTO newsrecord (`nauthor`,`ncontent`,`startTime`,`endUpdateTime`,`ntitle`,`ntypeid`) values(?,?,?,?,?,?)",obj);17         18       19     }
View Code

新增servlet

1 public void doPost(HttpServletRequest request, HttpServletResponse response) 2             throws ServletException, IOException { 3            4            request.setCharacterEncoding("utf-8"); 5            response.setContentType("text/html; charset=utf-8"); 6            //主题 7            int ntid=Integer.parseInt(request.getParameter("ntid")); 8          //标题 9            String ntitle=request.getParameter("ntitle");10           //作者 11           String nauthor=request.getParameter("nauthor");12           //摘要 13           String nsummary=request.getParameter("nsummary");14         //内容15           String ncontent=request.getParameter("nauthor");16         //上传图片17           18           19           String file=request.getParameter("file");  20           NewsWeb news=new NewsWeb(nauthor,ncontent,file,ntitle,ntid,nsummary);21           NewsWebDaoImpl nw=new  NewsWebDaoImpl();22           23           if(nw.addNewsWeb(news)){24               System.out.print("成功!");25               request.getSession().setAttribute("xi", "<>");26               response.sendRedirect(request.getContextPath()+"/newspages/admin.jsp");27              // out.print("");28           }else{29               System.out.print("失败!");30              request.getSession().setAttribute("xi", "<>");31               response.sendRedirect(request.getContextPath()+"/newspages/admin.jsp");32           }
新增Servlet
动态显示新闻标题内容
 
动态显示Servlet
1 public void doPost(HttpServletRequest request, HttpServletResponse response) 2             throws ServletException, IOException { 3             TopicDaoImpl dao=new    TopicDaoImpl(); 4              5             List
alltopic=dao.getAllTopic(); 6 7 request.setAttribute("Topiclist", alltopic); 8 String data=request.getParameter("tid"); 9 10 if (data!=null&&!data.equals("")) {11 int tid=Integer.parseInt(data);12 13 NewDaoImpl topicdao=new NewDaoImpl();14 List
list = topicdao.getNewsById(tid);15 16 request.setAttribute("newsList",list);17 18 }else {19 //处理新闻相关内容20 NewDaoImpl newsDao=new NewDaoImpl();21 List
newsList = newsDao.getTopNews();22 request.setAttribute("newsList", newsList);23 }24 //转向DoIndexServlet获取数据25 26 //准发到index.jsp27 request.getRequestDispatcher("/index.jsp").forward(request, response);28 }
View Code

动态显示实现类

1 public List
getAllTopic() { 2 Connection connection=getConnection(); 3 String sqlString="select typeid,typename from type"; 4 QueryRunner query=new QueryRunner(); 5 List
list=null; 6 try { 7 8 list=query.query(connection, sqlString, new BeanListHandler
(NewsType.class)); 9 10 System.out.println(list.get(0).getTypeName());11 } catch (SQLException e) {12 // TODO Auto-generated catch block13 e.printStackTrace();14 }15 return list;16 }17 18 19 public List
getTopNews() {20 Connection connection=getConnection();21 QueryRunner query=new QueryRunner();22 //select * from newsrecord where rownum<=3 orcal查询前三条语句23 String sqlString="select * from newsrecord where nid limit 3";24 List
list=null;25 try {26 list=query.query(connection, sqlString, new BeanListHandler
(News.class));27 } catch (SQLException e) {28 // TODO Auto-generated catch block29 e.printStackTrace();30 }31 return list;32 }

前端代码

1  
2
12
19
View Code
添加新闻主题

新增新闻类型Servlet
1 public void doPost(HttpServletRequest request, HttpServletResponse response) 2             throws ServletException, IOException { 3  4           request.setCharacterEncoding("utf-8"); 5           response.setContentType("text/html; charset=utf-8"); 6           String tname=request.getParameter("tname"); 7           NewsType newsType=new NewsType(tname); 8           NewsTypeDaoImpl ntdi=new NewsTypeDaoImpl(); 9           if (ntdi.addNewsType(newsType)) {10               request.getSession().setAttribute("xi", "<>");11           }else{12               System.out.println("");13               request.getSession().setAttribute("xi", "<>");14           }15           response.sendRedirect("/news/util/addnewstype.jsp");16           17     }
View Code

新增类型实现类

1 public boolean addNewsType(NewsType newsType){    2       return    executeUpdate("insert into type(typename)   3            values(?)", newsType.getTypeName());4     }
View Code

 

  

 

转载于:https://www.cnblogs.com/PGYXZ/p/4996244.html

你可能感兴趣的文章
走进JavaWeb技术世界7:Tomcat中的设计模式
查看>>
阅读mutate源码学习dplyr
查看>>
layui upload 额外参数上传
查看>>
我来悟微服务(3)-需求管理
查看>>
fatal error LNK1112: module machine type 'x64' conflicts with target machine type 'X86'
查看>>
SpringBoot-14-MyBatis预热篇,MySQL小结
查看>>
深度学习基础与技巧
查看>>
Firefox 将导入 Windows 根证书,避免与杀毒软件的冲突
查看>>
Windows搭建golang开发平台
查看>>
多线程基础篇(3)——初试锁
查看>>
利用WSS搭建学生作业平台
查看>>
刚进入win7系统就提示检测到一个硬盘问题的解决方法
查看>>
Python之配置日志模块logging
查看>>
指定目录的所有 *.gif 文件都重命名为 *.jpg
查看>>
为11.2.0.2 Grid Infrastructure添加节点
查看>>
Linux运维课程 第一阶段 重难点摘要(六)CISCO
查看>>
inotify结合rsync监控目录的实时变化
查看>>
pfSense book之硬件配置指南
查看>>
存储过程总结 2
查看>>
js parsefloat
查看>>